#include <iostream>
#include <esconu.h>
#include <fstream>
#include <string>

using namespace std;

const int MAX_ITEMS = 100;

// Structure to represent an item
struct Item {
    int code;
    string name;
    string brand;
    double price;
};

// Function to list all items
char listItems(const Item items[], int itemCount);

// Function to add a new item
char addItem(Item items[], int& itemCount);

// Function to delete an item by code
char deleteItem(Item items[], int& itemCount);

// Function to save items to a text file
char saveItemsToFile(const Item items[], int itemCount, const string& filename);

// Function to load items from a text file
char loadItemsFromFile(Item items[], int& itemCount, const string& filename);

int main() {
    Item items[MAX_ITEMS];
    int itemCount = 0;
    string filename = "inventory.txt"; // Name of the text file to save and load data

    // Load items from the file at program startup
    loadItemsFromFile(items, itemCount, filename);

    int choice;

    do {
        cout << "Don Churumusco Hardware Store" << endl;
        cout << "Option menu" << endl;
        cout << "1.- List items" << endl;
        cout << "2.- Add items" << endl;
        cout << "3.- Delete items" << endl;
        cout << "4.- Exit" << endl;
        cout << "Choose an option: ";
        cin >> choice;

        switch (choice) {
            case 1:
                BorraPantalla();
                listItems(items, itemCount);
                break;
            case 2:
                BorraPantalla();
                addItem(items, itemCount);
                break;
            case 3:
                BorraPantalla();
                deleteItem(items, itemCount);
                break;
            case 4:
                BorraPantalla();
                // Save items to the file before exiting
                saveItemsToFile(items, itemCount, filename);
                cout << "Exiting program." << endl;
                break;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    } while (choice != 4);

    return 0;
}

// Function to list all items
char listItems(const Item items[], int itemCount) {
    cout << "Don Churumusco Hardware Store" << endl;
    cout << "List of items in stock:" << endl;
    cout << "-------------------------------------------------- ---------------------" << endl;
    cout << "Code  Item                    Brand                  Price" << endl;
    cout << "-------------------------------------------------- ---------------------" << endl;

    for (int i = 0; i < itemCount; i++) {
        cout << items[i].code << ".    " << items[i].name << "          " << items[i].brand << "          " << items[i].price << endl;
    }

    cout << "-------------------------------------------------- ---------------------" << endl;

    return ' ';
}

// Function to add a new item
char addItem(Item items[], int& itemCount) {
    if (itemCount == MAX_ITEMS) {
        cout << "Inventory is full. Cannot add more items." << endl;
        return ' ';
    }

    Item newItem;
    cout << "Enter item code: ";
    cin >> newItem.code;
    cout << "Enter item name: ";
    cin.ignore();
    getline(cin, newItem.name);
    cout << "Enter item brand: ";
    getline(cin, newItem.brand);
    cout << "Enter item price: ";
    cin >> newItem.price;

    items[itemCount++] = newItem;
    cout << "Item added successfully!" << endl;

    return ' ';
}

// Function to delete an item by code
char deleteItem(Item items[], int& itemCount) {
    int codeToDelete;
    cout << "Enter the code of the item to delete: ";
    cin >> codeToDelete;

    for (int i = 0; i < itemCount; i++) {
        if (items[i].code == codeToDelete) {
            cout << "Item found:" << endl;
            cout << "Code: " << items[i].code << endl;
            cout << "Item: " << items[i].name << endl;
            cout << "Brand: " << items[i].brand << endl;
            cout << "Price: " << items[i].price << endl;

            char confirm;
            cout << "Are you sure you want to delete this item? (s/n): ";
            cin >> confirm;
            if (confirm == 's' || confirm == 'S') {
                for (int j = i; j < itemCount - 1; j++) {
                    items[j] = items[j + 1];
                }
                itemCount--;
                cout << "Item deleted successfully!" << endl;
            } else {
                cout << "Deletion canceled." << endl;
            }
            return ' ';
        }
    }

    cout << "Item not found." << endl;

    return ' ';
}

// Function to save items to a text file
char saveItemsToFile(const Item items[], int itemCount, const string& filename) {
    ofstream file(filename);
    if (!file) {
        cerr << "Error opening file for writing." << endl;
        return ' ';
    }

    for (int i = 0; i < itemCount; i++) {
        file << items[i].code << ',' << items[i].name << ',' << items[i].brand << ',' << items[i].price << endl;
    }

    file.close();

    return ' ';
}

// Function to load items from a text file
char loadItemsFromFile(Item items[], int& itemCount, const string& filename) {
    ifstream file(filename);
    if (!file) {
        cerr << "Error opening file for reading." << endl;
        return ' ';
    }

    itemCount = 0;
    Item item;

    while (file >> item.code) {
        file.ignore();
        getline(file, item.name, ',');
        getline(file, item.brand, ',');
        file >> item.price;
        items[itemCount++] = item;
    }

    file.close();

    return ' ';
}
